package com.hwtx.form;

import lombok.Data;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;

@Component
@Data
public class UserService {

    private ClassPathXmlApplicationContext applicationContext;

    private String misId;
    private String name;
    private String mis;
    private String avatar;

    public static void main(String[] args) {
        UserService userService = new UserService();
        userService.start();
    }

    public void start() {
        GenericApplicationContext context = new GenericApplicationContext();
        DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory();

        // 注册 OtherService
        beanFactory.registerBeanDefinition("otherService", new GenericBeanDefinition(BeanDefinitionBuilder.genericBeanDefinition(OtherService.class).getBeanDefinition()));

        // 注册 OrderService
        beanFactory.registerBeanDefinition("orderService", new GenericBeanDefinition(BeanDefinitionBuilder.genericBeanDefinition(OrderService.class).getBeanDefinition()));
        beanFactory.preInstantiateSingletons();
        // 启动容器，完成Bean的创建和依赖注入
        context.refresh();

        // 获取并使用 OrderService
        OrderService orderService = context.getBean("orderService", OrderService.class);
        orderService.invoke();
    }

    public void invoke() {
        OrderService orderService = applicationContext.getBean(OrderService.class);
        orderService.invoke();
    }
}
